package Inits

import (
	"diablo-gin/configs"
	"github.com/go-redis/redis"
	"strconv"
	"sync"
)

var CacheOnce sync.Once

var CacheDb *redis.Client

func CacheInit() *redis.Client {
	CacheOnce.Do(func() {
		client := redis.NewClient(&redis.Options{
			Addr: configs.CacheHost + ":" + strconv.Itoa(configs.CachePort),
			DB:   0, //redis默认有0-15共16个数据库，这里设置操作索引为0的数据库
		})
		_, err := client.Ping().Result()

		if err != nil {
			panic(err.Error())
		}

		CacheDb = client
	})
	return CacheDb
}
